﻿using DFGK.IService;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Security.Claims;
using System.Security.Cryptography;

namespace DFGK_WMS.WebAPI.Controllers.PC.基础数据
{
    /// <summary>
    /// 巷道
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class RoutewayController : BaseController
    {
        private readonly IRoutewayService routewayService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="routewayService"></param>
        public RoutewayController(IWebHostEnvironment environment, IRoutewayService routewayService) : base(environment)
        {
            this.routewayService = routewayService;
        }
        /// <summary>
        /// 添加巷道信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddRrouteway(Routeway obj)
        {
            if (string.IsNullOrEmpty(obj.Code) || obj.Code.Length <= 0)
                return Ok(BaseResult.Result("400", "编码不能为空！"));

            obj.CreateUserId = base.BaseGetNowUserId(); ;
            obj.CreateUserName = base.BaseGetNowUserName(); ;
            string message = "";
            bool IsOK = routewayService.AddRrouteway(obj, out message);
            if (IsOK)
            {           
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！" + message, "500"));
        }

        /// <summary>
        /// 删除巷道信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelRouteway(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！")); ;
            }
            bool IsOK = routewayService.DelRrouteway(id);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }

        /// <summary>
        /// 批量删除巷道信息
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelRoutewayList(long[] idList)
        {
            if (idList.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！")); ;
            }
            var daRes = true;
            foreach (var uid in idList)
            {
                 daRes = routewayService.DelRrouteway(uid);             
            }
            if (daRes)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }

        /// <summary>
        /// 修改巷道信息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdRouteway(Routeway obj)
        {
            if (obj.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if (string.IsNullOrEmpty(obj.Code))
            {
                return Ok(BaseResult.Result("400", "必填项不能为空！"));
            }
          
            obj.ModifyUserId = base.BaseGetNowUserId();
            obj.ModifyUserName = base.BaseGetNowUserName();
            string message = "";
            var result = routewayService.UpdRrouteway(obj, out message);
            if (result)
            {
                return Ok(BaseResult.Result(result, "修改成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "修改失败！编码已存在！", "500"));
        }

        /// <summary>
        /// 显示巷道
        /// </summary>
        /// <param name="xname"></param>
        /// <param name="wid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetRroutewayList(string? xname, long wid = 0, int pageIndex = 1, int pageSize = 20)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;

            var list = routewayService.GetRroutewayList(xname, wid, pageIndex, pageSize, out totalCount);

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 根据id获取信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetRoutewayById(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var obj = routewayService.GetRroutewayById(id);
            if (obj != null)
            {
                return Ok(BaseResult.Result(obj, "获取成功", "200"));
            }
            return Ok(BaseResult.Result(null, "获取失败！", "500"));
        }

        /// <summary>
        /// 根据库房获取巷道
        /// </summary>
        /// <param name="wid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetWhIdRoutewayList(long wid)
        {
            if (wid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var list = routewayService.GetWhIdRoutewayList(wid);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 根据货架获取巷道
        /// </summary>
        /// <param name="sid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetShelfRoutewayId(long sid)
        {
            if (sid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            int? code=routewayService.GetShelfRoutewayId(sid);
            if (code==null)
            {
                return Ok(BaseResult.Result(0, "查询失败！", "500", 0));
            }
            return Ok(BaseResult.Result(code, "查询成功！", "200", 0));
        }
    }
}
